package com.kpx.app.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.facebook.rebound.Spring;
import com.kpx.app.R;

/**
 * User: 陈聪(617909447@qq.com)
 * Date: 2016-06-24
 * Time: 18:09
 * FIXME
 */
public class AnimalUtils {

    /**
     * 添加商品到购物车动画效果
     *
     */
    public static void showBall(Context context, int[] startLocation, Spring mSpring, int[] endLocation) {
        ImageView ball = new ImageView(context);// buyImg是动画的图片，我的是一个小球（R.drawable.sign）
        ball.setImageResource(R.drawable.ball);// 设置buyImg的图片
        setAnim(ball, startLocation, context, mSpring, endLocation);// 开始执行动画
    }

    /**
     * 设置动画效果
     *
     * @param v 动画图片
     * @param startLocation 开始位置
     * @param context
     * @param mSpring
     * @param endLocation 结束位置
     */
    private static void setAnim(final View v, int[] startLocation, Context context, final Spring mSpring, int[] endLocation) {
        LinearLayout anim_mask_layout = (LinearLayout) createAnimLayout(context);
        anim_mask_layout.addView(v);//把动画小球添加到动画层
        final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation);

        // 计算位移
        int endX = 0 - startLocation[0] + endLocation[0] + 30;// 动画位移的X坐标
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
        TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(800);// 动画的执行时间
        view.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
                v.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
                springBackAnim(mSpring);
            }
        });
    }

    /**
     * 购物车回弹的动画效果
     * @param spring
     */
    public static void springBackAnim (final Spring spring){
        spring.setEndValue(1.5f);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                spring.setEndValue(1f);
            }
        }, 200);
    }

    /**
     * 创建动画布局
     *
     * @param context
     * @return
     */
    private static ViewGroup createAnimLayout(Context context) {
        ViewGroup rootView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(context);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    /**
     * 添加动画图片到动画布局
     *
     * @param parent
     * @param view
     * @param location
     * @return
     */
    private static View addViewToAnimLayout(final ViewGroup parent, final View view, int[] location) {
        int x = location[0];
        int y = location[1];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setLayoutParams(lp);
        return view;
    }

    /**
     * 超市订单列表条目显示动画
     * @param convertVeiw
     */
    public static void showItemAni(View convertVeiw) {
        AlphaAnimation animation = new AlphaAnimation((float) 0.1, 1);
        animation.setDuration(300);//设置动画持续时间
        animation.setRepeatCount(0);//设置重复次数
        animation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        animation.setStartOffset(0);//执行前的等待时间

        ScaleAnimation sanimation = new ScaleAnimation(0.5f, 1f, 0.5f, 1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sanimation.setDuration(300);//设置动画持续时间
        sanimation.setRepeatCount(0);//设置重复次数
        sanimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        sanimation.setStartOffset(0);//执行前的等待时间

        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(animation);
        set.addAnimation(sanimation);
        set.setDuration(400);// 动画的执行时间

        convertVeiw.startAnimation(set);
    }
}
